//
//  XJSearchTieZiViewController.m
//  XinJiangMall
//
//  Created by RaoBo on 2018/6/4.
//  Copyright © 2018年 Tzyang. All rights reserved.
//  搜索帖子


#import "XJSearchTieZiViewController.h"

//View
#import "RBSearchNaviBar.h"
#import "XJSearchTieziCell.h"

// model
#import "XJSearchTieZiModel.h"

@interface XJSearchTieZiViewController ()<UITableViewDelegate,UITableViewDataSource>

@property(nonatomic, strong) RBSearchNaviBar *searchNaviBarView;
@property(nonatomic, strong) NSMutableArray *dataArray;
@property(nonatomic, strong) UITableView *searchResultTv; // 搜索结果tableView。默认hidden = YES;

@end

@implementation XJSearchTieZiViewController

#pragma mark - 一 lazy
- (RBSearchNaviBar *)searchNaviBarView {
    if (!_searchNaviBarView) {
        _searchNaviBarView = [[RBSearchNaviBar alloc] initWithFrame:CGRectMake(0, KStatusBarHeight, KWidth, 64)]; // Y轴:去掉状态栏。
        _searchNaviBarView.searchBar.placeholder = @"请输入帖子标题";
        
        XJWeakSelf(self)
        _searchNaviBarView.searchBlock = ^(NSString *searchStr) {
//            XJLog(@"搜索内容:%@",searchStr);
            [weakself requestSearchTieziWithKeyword:searchStr];
        };
        
        _searchNaviBarView.cancelBlock = ^{
            XJLog(@"取消");
            [weakself.navigationController popViewControllerAnimated:YES];
        };
    }
    return _searchNaviBarView;
}

- (NSMutableArray *)dataArray {
    if (!_dataArray) {
        _dataArray = [NSMutableArray array];
    }
    return _dataArray;
}
- (UITableView *)searchResultTv {
    if (!_searchResultTv) {
        _searchResultTv = [[UITableView alloc] initWithFrame:CGRectMake(0, kStatusBarAndNavigationBarHeight, KWidth, KHeight - XJTopNavH) style:(UITableViewStylePlain)];
        _searchResultTv.hidden = YES; // 默认隐藏。
        
        _searchResultTv.delegate = self;
        _searchResultTv.dataSource = self;
        
        _searchResultTv.rowHeight = 140;
        _searchResultTv.separatorStyle = UITableViewCellSeparatorStyleNone;
        
        [_searchResultTv registerNib:[UINib nibWithNibName:@"XJSearchTieziCell" bundle:nil] forCellReuseIdentifier:@"XJSearchTieziCellId"];
        
    }
    return _searchResultTv;
}


#pragma mark - 二 生命周期
- (void)viewDidLoad {
    [super viewDidLoad];

    [self.navigationController setNavigationBarHidden:YES animated:YES];
    [self.view addSubview:self.searchNaviBarView];
    
    [self.view addSubview:self.searchResultTv];
}

- (void)viewWillDisappear:(BOOL)animated {
    [super viewWillDisappear:animated];
    [self.navigationController setNavigationBarHidden:NO animated:animated];
}

#pragma mark - 三 网络请求
- (void)requestSearchTieziWithKeyword:(NSString *)keyword{
    if ([NSString isEmptyString:self.city]) {
        XJLog(@"城市为空操作无效");
        return;
    }
   
    if ([NSString isEmptyString:keyword]) {
        XJLog(@"关键字为空 操作无效");
        return;
    }
    keyword = @"呵呵了";
    
    // 清空之间数据
    [self.dataArray removeAllObjects];
    
    [XJHttpTool requestInvitationFindCity:self.city keyword:keyword successH:^(id responseObjc) {
        NSData *jsonData = [NSJSONSerialization dataWithJSONObject:responseObjc options:NSJSONWritingPrettyPrinted error:nil];
        NSString *jsonStr = [[NSString alloc] initWithData:jsonData encoding:NSUTF8StringEncoding];
        NSLog(@"%@",jsonStr);
        
        NSString * code = [NSString stringWithFormat:@"%@",responseObjc[@"code"]];
        if ([code isEqualToString:@"0"]) {
            NSArray *tempArr = responseObjc[@"data"][@"list"];
            if (tempArr.count) {
                self.searchResultTv.hidden = NO;
                for (NSDictionary *dic in tempArr) {
                    
                    XJSearchTieZiModel *model = [XJSearchTieZiModel mj_objectWithKeyValues:dic];
                    [self.dataArray addObject:model];
                }
                
                // 刷新数据
                dispatch_async(dispatch_get_main_queue(), ^{
                    [self.searchResultTv reloadData];
                });
                
            }else {
                [MBProgressHUD showError:@"未搜索到内容"];
            }
        }else {
            self.searchResultTv.hidden = YES;
        }
        
    } failH:^(NSError *error) {
        self.searchResultTv.hidden = YES;

        [MBProgressHUD showError:[error localizedDescription]];
    }];
}

#pragma mark - 四 UITableViewDataSource
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.dataArray.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    XJSearchTieziCell *cell = [tableView dequeueReusableCellWithIdentifier:@"XJSearchTieziCellId"];
    
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    
    XJSearchTieZiModel *model = self.dataArray[indexPath.row];
    
    cell.model = model;
    
    return cell;
}

#pragma mark - 点击事件
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    
}
@end
